package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"os"
)

type UploadController struct {
	beego.Controller
}

// @Title 上传文件
// @Description 上传文件
// @Success 200 {"code":200,"msg":"上传成功","data":{"url":"url"}}
// @Params file formData file 文件FormData类型
// @router /upload [post]
func (self *UploadController) Post() {
	file, header, _ := self.GetFile("file")
	uploadPath := beego.AppConfig.String("uploadPath")
	getfilePath := beego.AppConfig.String("getfilePath")
	defer file.Close()
	error := self.SaveToFile("file", uploadPath+string(os.PathSeparator)+header.Filename)
	if error != nil {
		self.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "上传失败",
		}
		self.ServeJSON()
		self.StopRun()
	}
	self.Data["json"] = map[string]interface{}{
		"code": 200,
		"data": map[string]interface{}{
			"url": fmt.Sprintf("%s%s", getfilePath, header.Filename),
		},
		"msg": "上传成功",
	}
	self.ServeJSON()
	self.StopRun()
}
